package demo3;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 阿宾
 * Date: 2024-06-04
 * Time: 16:00
 */
class Student implements Comparable<Student>{
    public String name;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override//用来指定比较大小的方式
    public int compareTo(Student o) {
        //return this.age-o.age;//根据年龄大小比较
        if(this.age > o.age){
            return 1;
        } else if (this.age < o.age) {
            return -1;
        }else {
            return 0;
        }
    }
}
public class Test {

    public static void main(String[] args) {
        Student student1 = new Student("张三",18);
        Student student2 = new Student("李四",25);
        //比较两个对象
        System.out.println(student1.compareTo(student2));//即比较student1对象中的年龄和student2对象中的年龄
    }

}
